package datastore.gqlparser;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import org.apache.commons.lang.builder.ToStringBuilder;

import datastore.db.Key;

public class ParsedQuery {
    protected Class<?> kind;
    protected LinkedHashMap<String, Object> filters = new LinkedHashMap<String, Object>();
    protected List<String> orderBys = new ArrayList<String>();
    protected Object ancestor;
    protected long offset;
    protected long count;

    public Class<?> getKind() {
        return kind;
    }

    public void setKind(String kind) {
        try {
            this.kind = Class.forName(kind);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    public long getOffset() {
        return offset;
    }

    public void setOffset(String offset) {
        this.offset = Long.parseLong(offset);
    }

    public long getCount() {
        return count;
    }

    public void setCount(String count) {
        this.count = Long.parseLong(count);
    }
    
    public void filter(String property, String operator, Object value) {
        filters.put(property+" "+operator, value);
    }
   
    public void ancestor(String encodedKey) {
        ancestor = new Key(encodedKey);
    }
    
    public void ancestor(List<String> path) {
        ancestor = Key.fromPath(path);
    }
    
    public void ancestorBound(String bound) {
        ancestor = bound;
    }
    
    public void orderBy(String orderBy) {
        orderBys.add(orderBy);
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }
}
